import {onMounted, ref} from "vue";
import admin_service from "../../service/admin_service";
import {ElMessage} from "element-plus";
import {useRouter} from "vue-router";
import admin from "../../service/admin_service";

export default function ArticleStorehouse() {
    const router = useRouter()
    let loading = ref(false)
    let numTotal = ref(0)
    onMounted(() => {
        loading.value = true
        admin_service.getArticleList().then(r => {
            let data = r.data
            loading.value = false
            if (data.code === 200) {
                numTotal.value = data['msg']['total']
                for (let i = 0; i < data['msg'].data.length; i++) {
                    let tmpData = data['msg'].data[i]
                    let tmpObj = {}
                    tmpObj.id = tmpData['ID']
                    tmpObj.title = tmpData['Title']
                    tmpObj.class = tmpData['Class']
                    tmpObj.content = tmpData['Content'].substring(1, 30)
                    tmpObj.author = tmpData['Author']
                    ArticleContent.value.push(tmpObj)
                }
            } else {
                ElMessage.error("获取文章错误！")
            }
        })
        // 获取分类信息
        admin_service.getSorts().then(r => {
            let data = r.data
            SortList.value = []
            for (let i = 0; i < data['msg'].length; i++) {
                let dataList = {}
                dataList.label = data['msg'][i]['Name']
                dataList.value = data['msg'][i]['UrlName']
                SortList.value.push(dataList)
            }
        })
    })
    let ArticleContent = ref([])
    const newPages = (e) => {
        admin_service.pagingFind({pages_size: 10, this_page: e}).then(r => {
            let data = r.data
            if (data.code === 200) {
                ArticleContent.value = []
                numTotal.value = data['msg']['total']
                for (let i = 0; i < data['msg'].data.length; i++) {
                    let tmpData = data['msg'].data[i]
                    let tmpObj = {}
                    tmpObj.id = tmpData['ID']
                    tmpObj.title = tmpData['Title']
                    tmpObj.class = tmpData['Class']
                    tmpObj.content = tmpData['Content'].substring(1, 30)
                    tmpObj.author = tmpData['Author']
                    ArticleContent.value.push(tmpObj)
                }
            } else {
                ElMessage.error("获取文章错误！")
            }
        })
    }
    const deleteArticleBtn = (e) => {
        let data = ArticleContent.value[e]
        admin_service.deleteArticle({id: data.id, title: data.title}).then(r => {
            if (r.data.code === 200) {
                ElMessage.success("删除成功")
                ArticleContent.value.splice(e, 1)
            } else {
                ElMessage.error(`删除失败：${r.data['msg']}`)
            }
        })
    }
    let drawerStatus = ref(false)
    let editArticleTitle = ref('')
    // 全局变量，当前编辑文章ID
    let ArticleID = 0
    const editArticleBtn = (e) => {
        let data = ArticleContent.value[e]
        drawerStatus.value = true
        SortContent.value = data['class']
        editArticleTitle.value = data['title']
        admin_service.findArticleInfo({id: data.id, title: data.title}).then(r => {
            let res = r.data
            content.value = res['msg']
            if (content.value !== '') {
                ArticleID = data.id
            }
        })
    }
    const updateArticleBtn = () => {
        // 文章id
        // ArticleID
        let data = {
            id: ArticleID,
            title: editArticleTitle.value,
            class: SortContent.value,
            Content: content.value
        }
        admin_service.updateArticle(data).then(r=>{
            let data = r.data
            if (data.code === 200){
                ElMessage.success("更新成功")
                router.go(0)
            }
        })
    }
    let dialogVisible = ref(false)
    const drawerClose = () => {
        dialogVisible.value = true
    }
    const closeBtn = () => {
        dialogVisible.value = false
        drawerStatus.value = false
    }
    let SortContent = ref('')
    let SortList = ref([
        {
            value: 'Option1',
            label: 'Option1',
        },
        {
            value: 'Option2',
            label: 'Option2',
        }
    ])
    // 文章编辑信息
    let content = ref('')
    let imageList = ref([])
    const UploadImage = (e, image, files) => {
        console.log('e:', e, 'image"', image, 'files:', files)
    }
    const handleCopyCodeSuccess = (code) => {
        console.log(code)
    }
    const saveBtn = (text, html) => {
        console.log('text:', text, 'html:', html)
    }

    const handleClose = () => {
        dialogVisible.value = false
        drawerStatus.value = false
    }
    return {
        content,
        imageList,
        UploadImage,
        handleCopyCodeSuccess,
        saveBtn,
        ArticleContent,
        loading,
        numTotal,
        newPages,
        deleteArticleBtn,
        editArticleBtn,
        drawerStatus,
        drawerClose,
        dialogVisible,
        closeBtn,
        SortList,
        SortContent,
        editArticleTitle,
        updateArticleBtn,
        handleClose
    }
}